package charpter01_basic.lession05_impl_producter_consumer;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * 通过阻塞队列来实现生产者消费者
 */
public class ProducterConsumer1 {

    private static final BlockingQueue<Integer> BLOCKING_QUEUE = new ArrayBlockingQueue<>(10);

    public static void main(String[] args) {
        new Thread(() -> {
            while (true) {
                try {
                    BLOCKING_QUEUE.put(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("生产1个, 目前有" + BLOCKING_QUEUE.size() + "个");
            }
        }).start();

        new Thread(() -> {
            while (true) {
                Integer poll = null;
                try {
                    poll = BLOCKING_QUEUE.take();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(poll + "消费1个， 还有" + BLOCKING_QUEUE.size());
            }
        }).start();
    }
}
